var obj = new URLSearchParams(location.search)
var id = obj.get('id')
console.log(id)
$.ajax({
    type:'get',
    url:`/posts/${id}`,
    success:function(res){
        console.log(res)
        var html = template('detailTpl',res)
        $('.article').html(html)
    }
})

//点赞功能
$('.article').on('click','#like',function(){
    $.ajax({
        type:'post',
        url:`/posts/fabulous/${id}`,
        success:function(res){
            alert('点赞成功')
        }
    })
})

var state = 0;
//获取后台设置的参数，通过是否开启评论来判断当前表单隐藏还是显示
$.ajax({
    type:'get',
    url:'/settings',
    success:function(res){
        if(res.review == true){
            state = 0;
        }else{
            state = 1;
        }
        if(res.comment){
            $('#commentForm').show()
        }
    }
})


//添加评论功能
$('#commentForm').on('submit',function(){
    var content = $(this).find('textarea').val();
    $.ajax({
        type:'post',
        url:'/comments',
        data:{
            post:id,
            content:content,
            state:state 
        },
        success:function(){
            alert('评论成功')
            location.reload()
        }
    })
    return false;
})